namespace NFreeMarker.Core
{
    /**
     * Represents a &lt;stop&gt; instruction to abort template processing.
     * @author <A HREF="mailto:jon@revusky.com">Jonathan Revusky</A>
     */

    public class StopInstruction : TemplateElement // NOTE: Was internal
    {
        private readonly Expression _exp;

        internal StopInstruction(Expression exp)
        {
            _exp = exp;
        }

        internal override void Accept(Environment env)
        {
            if (_exp == null)
            {
                throw new StopException(env);
            }
            throw new StopException(env, _exp.GetStringValue(env));
        }

        public override string GetCanonicalForm()
        {
            string expString = _exp == null ? "" : " " + _exp.GetCanonicalForm();
            return "<#stop" + expString + "/>";
        }

        public override string GetDescription()
        {
            return "stop" + " [" + GetStartLocation() + "]";
        }
    }
}